/***********************************************************
 *  File_Name  : XJOCCI.h
 *  File_Path   : 
 *  File_Encode : UTF8
 *  Description : Oracle DB OCCI 操作类
 *  
 *  OS : Linux, UNIX
 *  LastVersion  : 20120914
 *      
 *  Author      : LiXiujie(李秀杰)
 *  Date        : 2012-9-14
 *  Version     : 20120914
 *  Description : 
 *                
 *  Edit Author : 
 *  Edit Date   : 
 *  Version     : 
 *  Description :
 *                
 ***********************************************************/
#ifndef XJOCCI_H_
#define XJOCCI_H_

#include <string>
#include <vector>
#include <set>
#include <map>
#include <iostream>
#include <stdlib.h>
#include <occi.h>

using namespace oracle::occi;
using namespace std;
namespace XJUtilClass{

typedef void (*XJOCCIExceptionProcess)(SQLException &ex,const string &strSql, void *pData);
typedef void (*XJOCCIPrintSQL)(const string &strSql);

/*
class XJOCCI;
class XJOCCIResultSet;

class XJOCCIResultSet{
public:
	XJOCCIResultSet(){
		m_pRS = NULL;
		m_pStmt = NULL;
		m_pConn = NULL;
		m_bFreeConn = false;
		m_pXJOCCI = NULL;
	}
	~XJOCCIResultSet(){
		DeleteRS();
	}
	friend class XJOCCI;
public:
	ResultSet* GetRS(){ return m_pRS;}
	void DeleteRS(){
		if (m_pStmt && m_pRS){
			m_pStmt->closeResultSet(m_pRS);
			m_pRS = NULL;
		}
		if (m_pConn && m_pStmt){
			m_pConn->terminateStatement(m_pStmt);
			m_pStmt = NULL;
		}
		if (m_pConn && m_bFreeConn){
			m_pXJOCCI->CloseConnection(m_pConn);
			m_pConn = true;
			m_bFreeConn = false;
		}
	}
private:
	XJOCCI     *m_pXJOCCI;
	ResultSet  *m_pRS;
	Statement  *m_pStmt;
	Connection *m_pConn;
	bool m_bFreeConn;
};
*/
class XJOCCI {
public:
	XJOCCI();
	XJOCCI(const char *szDBUser,const char *szDBPassWD,const char *szDBName,
		const bool &bAutoConn = true,const bool &bAutoCommit=true);
	virtual ~XJOCCI();
	//friend class XJOCCIResultSet;
public:
	const char*	GetDBName();
	void	SetDBName(const char* szDBName);
	const char*	GetDBUser();
	void	SetDBUser(const char* szDBUser);
	const char*	GetDBPassWD();
	void	SetDBPass(const char* szDBPassWD);
	int	GetMaxConn();
	void	SetMaxConn(int nMaxConn);
	int	GetMinConn();
	void	SetMinConn(int nMinConn);
	int	GetIncrConn();
	void	SetIncrConn(int nIncrConn);

	bool CreateDBConnPool();
	void CloseDBConnPool();
	Connection* CreateConnection();
	void CloseConnection(Connection *pConn);

	Connection* GetAutoConn();
	Connection* SetAutoConn(bool bAutoConn);


	Environment* GetEnv();
	ConnectionPool* GetConnPool();
/*
	bool SelectData(const string& strSql, XJOCCIResultSet &xjRS);
	bool SelectData(Connection* pConn, const string& strSql, XJOCCIResultSet &xjRS);
*/
	int AddData(const string& strTableName, const string&, const string& strValues);
	int AddData(Connection* pConn,const string& strTableName, const string&, const string& strValues);
	int ModifyData(Connection* conn,const string& strTableName, const string& strFields, const string& strConditions);
	int ModifyData(const string& strTableName, const string& strFields, const string& strConditions);
	int RemoveData(Connection* pConn,const string& strTableName, const string& strConditions);
	int RemoveData(const string& strTableName, const string& strConditions);

	int Execute(const string& strSql);
	int Execute(Connection* pConn,const string& strSql);


	const Environment* GetEnv()const{return this->m_pEnv;}
	bool IsAutoCommit(){return m_bAutoCommit;}
	void SetAutoCommit(bool bAutoCommit){m_bAutoCommit=bAutoCommit;}

	void Commit(Connection* pConn){pConn->commit();}
	void Rollback(Connection* pConn){pConn->rollback();}

	void SetExceptionProcessFun(XJOCCIExceptionProcess pFun, void *pData){
		m_pExceptionProcessFun = pFun;
		m_pXJOCCIExceptionProcessData = pData;
	}
	void SetPrintSQLFun(XJOCCIPrintSQL pFun){
		m_pPrintSQLFun = pFun;
	}
protected:
	void ExceptionProcess(SQLException &ex,const string &strSql = "");
	void PrintSQL(const string &strSql = "");
private:
	bool InitConnectionPool();
	bool DestroyConnectionPool();
private:
	Environment    *m_pEnv;
	ConnectionPool *m_pConnPool;
	Connection     *m_pConn;

	string  m_strDBName;
	string  m_strDBUser;
	string  m_strDBPassWD;

	int m_nMaxConn;
	int m_nMinConn;
	int m_nIncrConn;

	bool m_bAutoCommit;
	bool m_bAutoConn;

	XJOCCIExceptionProcess m_pExceptionProcessFun;
	void *m_pXJOCCIExceptionProcessData;
	XJOCCIPrintSQL         m_pPrintSQLFun;

};
}// namespace XJUtilClass{
#endif /* XJOCCI_H_ */
